import cv2
import xml.etree.ElementTree as ET
def markImage(xml_src, img_src):
    root = ET.parse(xml_src).getroot()
    count = 0

    img = cv2.imread(img_src)
    # 遍历xml文件中object属性
    for obj in root.iter('object'):
        name = obj.find('name').text
        x0 = int(obj.find('bndbox/xmin').text)
        y0 = int(obj.find('bndbox/ymin').text)
        x1 = int(obj.find('bndbox/xmax').text)
        y1 = int(obj.find('bndbox/ymax').text)

        #在图像上绘制矩形框
        cv2.rectangle(img,(x0,y0),(x1,y1),(0,0,255),1)

    cv2.imwrite('res.jpg', img)
    cv2.imshow('image',img)
    cv2.waitKey(0)

# markImage("C:\\Users\\qaz13\\Desktop\\object-detection-augmentation-main\\VOCdevkit\\VOC2007\\Annotations\\9.xml",
#           "C:\\Users\\qaz13\\Desktop\\object-detection-augmentation-main\\VOCdevkit\\VOC2007\\JPEGImages\\9.jpg")

markImage("C:\\Users\\qaz13\\Desktop\\yolov5-6.0\\My_Datasets\\train\\label_xml\\71.xml",
          "C:\\Users\\qaz13\\Desktop\\yolov5-6.0\\My_Datasets\\train\\images\\71.jpg")